<?php

class indexControl extends Control {

    //主页显示
    public function index() {


        //用户信息显示
   if (isset($_SESSION['user'])) {
            $db = M('user');

            $uid = $_SESSION['user']['uid'];
            //采集数量
            $user_cai = M('user_img');
            $cai_con = $user_cai->where("uid='$uid'")->count();
            $this->assign("cai_con", "$cai_con");
            //新建画板显示
            $cat = M('category');
            $cata = $cat->select();
            $this->assign('cata', $cata);
            //上传采集
            $chaiji = M('huaban');
            $cha = $chaiji->where("uid='$uid'")->all();
            $hua_con = $chaiji->where("uid='$uid'")->count();
            $this->assign("hua_con", $hua_con);
            $this->assign("chaiji", $cha);
            $uid = $_SESSION['user']['uid'];
            $dat = $db->where("uid='$uid'")->all();
            $this->assign("dat", $dat[0]);
        }
        $this->display();
    }

    //登录验证
    public function log() {
        if ($_POST['username'] != '') {
            $username = $_POST['username'];
            $pwd = md5($_POST['password']);
            $db = M('user');
            $dat = $db->where("email='$username'")->find();
          if ($dat['email'] == $username && $dat['password'] == $pwd) {
                $_SESSION['user'] = $dat;
                $this->go('index');
            } else {
                $this->error('用户名或密码错误');
            }
        } else {
            $this->success('用户名不能为空');
        }
    }

    //异步验证用户是否存在
    public function reg_yan() {
        $aa = $_POST['dat'];
        $db = M('user');
        $data = $db->where("email='$aa'")->count();
        if ($data == 0) {
            echo 1;
        } else {
            echo 2;
        }
    }

    public function reg() {
        $this->display();
    }

    //注册处理
    public function regs() {
        $_POST['regtime'] = time();
        $_POST['password'] = md5($_POST['password']);
        if (isset($_POST['user_img'])) {
            $_POST['user_img'] = $_POST['user_img'][1][0];
        } else {
            $_POST['user_img'] = "upload/img/def.png";
        }
        $db = M('user');
        if ($ds = $db->add()) {
            $session = $db->where("uid='$ds'")->find(); 
            $_SESSION['user'] = $session;
            //如果注册成功新建一个画板
            $ds = M('huaban');
            $_POST['uid'] = $_SESSION['user']['uid'];
            $_POST['title'] = '默认画板';
            if ($ds->add()) {
                $this->success('注册成功', 'index');
            }
        } else {
            $this->error('注册失败');
        }
    }

    public function out() {
        $this->session_destroy();
        $this->success('您已成功退出');
    }

    //账号设置
    public function edit_user() {
        $db = M('user');
        // p($_SESSION['user']);
        if (isset($_SESSION['user'])) {
            $uid = $_SESSION['user']['uid'];
            $das = $db->where("uid='$uid'")->all();
            $this->assign("das", $das[0]);
        }
        $this->display();
    }

    //账号设置执行
    public function edit_user_p() {
        $uid = $_SESSION['user']['uid'];
        $_POST['uid'] = $uid;
        $db = M('user');
        if ($db->update()) {
            $this->success('修改成功');
        } else {
            $this->error('修改失败');
        }
    }

    //密码修改
    public function pwd() {
        $this->display();
    }

    public function pwds() {
        if (isset($_POST['password1'])) {
            $pwd1 = md5($_POST['password1']);

            $pwd2 = md5($_POST['password2']);
            $db = M('user');
            $uid = $_SESSION['user']['uid'];
            $_POST['uid'] = $uid;
            $set = $db->where("uid='$uid'")->find();

            if ($set['password'] == $pwd1) {
                $_POST = array('uid' => "$uid", 'password' => "$pwd2");

                if ($db->update()) {
                    $this->success('修改成功', 'edit_user');
                } else {
                    $this->error('当前密码没有变化');
                }
            } else {
                $this->error('当前密码填写错误请重新填写');
            }
        }
    }

    //新建画板
    public function createhuaban() {
        if (isset($_POST['tid'])) {
            $_POST['uid'] = $_SESSION['user']['uid'];
            $db = M('huaban');
            if ($db->add()) {
                $this->success('创建成功');
            }
        }
    }

    //上传采集
    public function chaiji() {
        if (isset($_POST['img'])) {
            $_POST['img'] = $_POST['img'][1][0];
        }
        $_POST['uid'] = $_SESSION['user']['uid'];
        if (isset($_POST['title'])) {
            $_POST['title'] = trim($_POST['title']);
        }
        $_POST['addtime']=  time();
        $db = M('user_img');
        if ($db->add()) {
            $this->success('上传成功');
        }
    }

    public function data_ajax() {
     $page = empty($_POST['page']) ? 2 : $_POST['page'];
        // static $con='';
        if ($page) {
            $con = ($page - 1) * 10;
            $db = M('user_img');
            $sql="	select * from h_user_img inner join h_user on h_user_img.uid=h_user.uid inner join h_huaban on h_user_img.hid=h_huaban.hid order by cid desc limit $con,10; ";
              $data=$db->query($sql);
        // $data = $db->limit($con, 10)->order("cid desc")->all();
                p($data);exit;
                $this->display();
            echo json_encode($data);
     
        }
    }
    public function img_info(){
        $this->display();
        
    }
    //转采
    public function zhuancai(){
     if(!empty($_SESSION['user']['uid'])){
          if(!empty($_POST['cid'])){
      $cid=$_POST['cid'];
     $user=M('user_img');
     $dat=$user->where("cid='$cid'")->find();
     $wid=$_SESSION['user']['uid'];
     $uid=$dat['uid'];
     if($wid==$uid){
        echo "<script>alert('您已经拥有了这个采集无需采集');history.go(-1);</script>";
     }else{
      $_POST['uid']=$dat['uid'];
      $_POST['zhuantime']=time();
      $db=M('user_caiji');
      if($db->add()){
         $this->success('转采成功');
      }  
    }
  }
     }else{
         $this->error('您还没有登录');
     }
    
  
  
    }
}

?>